#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os

from controller.pptBaseController import PptBaseController
from models import db
from utils import commons
from utils.loggings import loggings
from utils.response_code import RET


class PptBaseService(PptBaseController):
    upload_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'uploads', 'ppt')

    @classmethod
    def get_ppt_location(cls) :
        """
        根据用户ID和PPT ID获取PPT文件的存储路径
        :param user_id: 用户ID
        :param ppt_id: PPT ID
        :return: PPT文件的存储路径，如果文件不存在返回 None
        """

        # 假设文件存储在 uploads/user_id/ppt/ 文件夹下
        ppt_location = os.path.join(os.path.dirname(os.path.dirname(__file__)), location)

        # 检查文件是否存在
        if os.path.exists(ppt_location):
            return ppt_location
        else:
            return None

    @classmethod
    def get_chat_list(cls):
        """
        获取聊天列表

        :return:
        """
        from flask import g
        user_id = g.user['user_id']
        res_chat_list = []
        try:

            ppt_base_info = db.session.query(cls).order_by(cls.create_time.desc()).filter(
                *[cls.user_id == user_id, cls.is_deleted == 0])

            res = ppt_base_info.all()
            res = commons.query_to_dict(res)

            for chat in res:
                res_chat_list.append({
                    'ppt_id': chat['ppt_id'],
                    'chat_root_id': chat['chat_root_id'],
                    'user_id': chat['user_id'],
                    'title': chat['title'],
                    'status': chat['status'],
                    'gen_type': chat['gen_type'],
                    'create_time': chat['create_time'],
                })
            return {'code': RET.OK, 'message': '获取聊天列表成功', 'data': res_chat_list}
        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': '获取聊天列表失败', 'data': {'error': str(e)}}
